package LinkList;

public class MyLinkList implements IList{
    static class ListNode{
        int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode cur=new ListNode(data);
        if(head==null){
            head=cur;
            return;
        }
        cur.next=head;
        head.prev=cur;
        head=cur;
        cur.prev=null;
    }

    @Override
    public void addLast(int data) {
        ListNode cur=new ListNode(data);
        if(head==null){
            head=cur;
            return;
        }
        ListNode last=head;
        while(last.next!=null){
            last=last.next;
        }
        last.next=cur;
        return;
    }

    @Override
    public void addIndex(int index, int data) {
        int len=size();
        if(index<0||index>len){
            System.out.println("插入位置不合法");
        }
        if(index==0){
            addFirst(data);
        }
        if(index==len) {
            addLast(data);
        }
        ListNode cur=head;
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        ListNode node=new ListNode(data);
        cur.next.prev=node;
        node.next=cur.next;
        node.prev=cur;
        cur.next=node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        ListNode curN=findOfIndex(key);
        if (curN==null){
            return;
        }
        curN.prev.next=curN.next;
        curN.next.prev=curN.prev;
        return;
    }

    public ListNode findOfIndex(int key){
        ListNode cur=head;
        if(head==null){
            return null;
        }
        while(cur!=null){
            if(cur.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return cur;
    }

    @Override
    public void removeAllKey(int key) {
        if(head==null){
            return;
        }
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                cur.prev.next=cur.next;
                cur.next.prev=cur.prev;
                cur=cur.next;
            }else {
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
            head.prev=null;
        }
    }

    @Override
    public int size() {
        ListNode cur=head;
        int count=0;
        while (cur!=null){
            cur=cur.next;
            count++;
        }

        return count;
    }

    @Override
    public void clear() {
        ListNode node=head;

    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null) {
            System.out.print(cur.val + " ");
            cur=cur.next;
        }
        System.out.println();
    }
}
